package com.example.cardgame

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
    var score=-1000;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }

        val game: Button=findViewById(R.id.game)
        val modify: Button=findViewById(R.id.modify)

        game.setOnClickListener{
            val intent= Intent(this, GameActivity::class.java)
            startActivityForResult(intent,0)
        }

        modify.setOnClickListener{
            val intent= Intent(this, ModifyActivity::class.java)
            startActivityForResult(intent,0)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==0){
            if(resultCode==1){
                val num= data?.getStringExtra("THIRD_MSG")!!.toInt()
                if (score<num){
                    var scores: TextView=findViewById(R.id.score)
                    scores.text=num.toString()
                }
                score=num
            }
            if (resultCode==2){
                var str= data?.getStringExtra("THIRD_MSG")?.split(",")
                str?.get(0)?.length?.let {
                    if(it >0){
                        val name=findViewById<TextView>(R.id.name)
                        name.text=str[0]
                    }
                }
                str?.get(1)?.length?.let {
                    if(it >0){
                        val phone=findViewById<TextView>(R.id.Email)
                        phone.text=str[1]
                    }
                }
                str?.get(2)?.length?.let {
                    if(it >0){
                        val gender=findViewById<TextView>(R.id.gender)
                        gender.text=str[2]
                    }
                }
            }
        }
    }
}
